import { NameGradeCell } from '@/components'
import { ServiceList } from '@/store/constant'
import { ServiceStatusEnum } from '@/types/enum'
import { GridColDef, GridRenderCellParams, GridValueGetterParams } from '@mui/x-data-grid-pro'
import { IconCheck, IconX, IconRobot } from '@tabler/icons-react'
import UpdateServiceButton from './UpdateServiceButton'
interface Options {
  refresh: () => void
}
const useColumns = ({ refresh }: Options) => {
  const columns: GridColDef[] = [
    {
      field: 'name',
      headerName: '职级 + 用户名',
      width: 200,
      renderCell(params) {
        const { row } = params
        return <NameGradeCell row={row} copyKey="star_id" />
      }
    },
    {
      minWidth: 100,
      field: 'robot_switch',
      renderHeader() {
        return (
          <Stack alignItems="center" direction="row">
            <IconRobot />
            <Typography variant="button">机器银</Typography>
          </Stack>
        )
      },
      editable: true,
      type: 'boolean',
      valueGetter(params: GridValueGetterParams) {
        const { row } = params
        return get(row.robot, 'robot_status') !== ServiceStatusEnum.Close
      },
      renderCell(params: GridRenderCellParams) {
        const { value } = params
        return <IconButton color={value ? 'success' : 'error'}>{value ? <IconCheck /> : <IconX />}</IconButton>
      }
    },
    ...ServiceList.map((cell) => {
      const { label, type } = cell
      return {
        minWidth: 100,
        field: `status_${type}`,
        headerName: `${label}`,
        editable: true,
        type: 'boolean',
        valueGetter(params: GridValueGetterParams) {
          const { row } = params
          const status = find(row.services, { type: type })?.status
          return ![ServiceStatusEnum.Shutdown, ServiceStatusEnum.Suspend].includes(status)
        },
        renderCell(params: GridRenderCellParams) {
          const { value } = params
          return <IconButton color={value ? 'success' : 'error'}>{value ? <IconCheck /> : <IconX />}</IconButton>
        }
      }
    }),
    ...ServiceList.map((cell) => {
      const { label, type } = cell
      return {
        minWidth: 120,
        field: `price_${type}`,
        headerName: `${label}_price`,
        type: 'number',
        editable: true,
        valueGetter(params: GridValueGetterParams) {
          const { row } = params
          return find(row.services, { type: type })?.price
        },
        renderCell(params: GridRenderCellParams) {
          const { value } = params
          return <Typography variant="button">{value}</Typography>
        }
      }
    }),
    {
      field: 'actions',
      headerName: '操作',
      width: 100,
      renderCell(params) {
        const { row } = params
        return <UpdateServiceButton row={row} refresh={refresh} />
      }
    }
  ]

  return columns
}

export default useColumns
